﻿namespace MSRI.SmsSessions
{
    using MSRI.HelperClasses;
    using System;
    using System.Collections.Generic;

    public class SmsSession
    {
        private bool _isDeleted;
        private object _sessionData;
        private string _sessionName;
        private DateTime _sessionStartTime;
        private List<SmsSessionContact> _SmsSessionContacts;
        private double _timeInterval;
        private SmsSessionDelegate SessionDelegate;

        public SmsSession()
        {
            this._sessionName = null;
            this._SmsSessionContacts = new List<SmsSessionContact>();
            this._sessionStartTime = DateTime.Now;
            this._isDeleted = false;
            this.SessionDelegate = null;
        }

        public SmsSession(SmsSession SessionItem)
        {
            this._sessionName = SessionItem.SessionName;
            this._SmsSessionContacts = new List<SmsSessionContact>(SessionItem.SessionContacts);
            this._sessionStartTime = SessionItem.SessionStartTime;
            this._isDeleted = SessionItem.IsDeleted;
            this.SessionDelegate = null;
        }

        public void AttachSessionDelegate(SmsSessionDelegate FunctionName)
        {
            this.SessionDelegate = new SmsSessionDelegate(FunctionName.Invoke);
        }

        public SmsResponseClass CallSessionDelegate(SmsClass SmsItem)
        {
            if (this.SessionDelegate != null)
            {
                return this.SessionDelegate(this._sessionData, SmsItem);
            }
            return null;
        }

        public bool Contains(SmsSessionContact ContactItem)
        {
            for (int i = 0; i < this._SmsSessionContacts.Count; i++)
            {
                if (this._SmsSessionContacts[i].Mobile == ContactItem.Mobile)
                {
                    return true;
                }
            }
            return false;
        }

        public void ResetTimer()
        {
            this._sessionStartTime = DateTime.Now;
            this._isDeleted = false;
        }

        public bool IsDeleted
        {
            get
            {
                if (!this._isDeleted)
                {
                    TimeSpan span = (TimeSpan) (DateTime.Now - this._sessionStartTime);
                    if (span.TotalSeconds >= this._timeInterval)
                    {
                        this._isDeleted = true;
                    }
                }
                return this._isDeleted;
            }
        }

        public List<SmsSessionContact> SessionContacts
        {
            get
            {
                return this._SmsSessionContacts;
            }
        }

        public string SessionName
        {
            get
            {
                return this._sessionName;
            }
            set
            {
                this._sessionName = value;
            }
        }

        public DateTime SessionStartTime
        {
            get
            {
                return this._sessionStartTime;
            }
        }

        public object SessionUserData
        {
            get
            {
                return this._sessionData;
            }
            set
            {
                this._sessionData = value;
            }
        }

        public double TimeInterval
        {
            get
            {
                return this._timeInterval;
            }
            set
            {
                this._timeInterval = value;
            }
        }

        public double TimeLeft
        {
            get
            {
                if (!this._isDeleted)
                {
                    TimeSpan span = (TimeSpan) (DateTime.Now - this._sessionStartTime);
                    if (span.TotalSeconds < this._timeInterval)
                    {
                        return (this._timeInterval - span.TotalSeconds);
                    }
                    this._isDeleted = true;
                }
                return -1.0;
            }
        }
    }
}

